<%@ page language="java" session="true" errorPage="ElderhealthError.jsp"%>
<%@ page import="com.elderhealth.client.Client"%>
<%
/*
 * Source:   ViewClient.jsp
 * Purpose:  This page will pull the corrent Client out of the already passed
 *           client objects and display their attributes to the screen.
 */
%>

<html>
<head>
<link rel="stylesheet" type="text/css" href="elderhealth.css">
<!-- Scripting Portion -->
<script type="text/javascript" src="elderhealth.js"></script>
</head>
<body>

<%@ include file="ElderhealthHeader.jsp" %>

<%
/* Intialize variables */
String errorCode;
String errorMessage;
String redirectURL;
System.out.println("In view client page");
try
{
  /* Retrieve client id from the passed parameters */
  int selectedClientID = Integer.parseInt(request.getParameter("selectedClient"));

  /* Retrieve clients and present them */
  Client[] clientResults = (Client[])session.getAttribute("results"); 
%>

<table border=0 align="left" border=0 cellpadding=0 cellspacing=0 width="600">
    <tr class="contactHeaderText"><td height="18" align="left" valign="middle" style="PADDING-LEFT:10px">View Client</td></tr>
    <tr><td class="layoutLine" valign="middle" align="left" width="302" height="1" colSpan="4"></td></tr>
    <tr class="contactBar1">
	  <td align="left" valign="middle" style="PADDING-LEFT:10px">

<%
/* Initialize the client variable */
Client selectedClient = new Client();

/* Check if this page was called by the search page or the GetWorkerSchedule.jsp */
if (clientResults == null || clientResults.length == 0)
{
  /* Call to jsp must have been made by GetWorkerSchedule.jsp, 
   * query client matching selectedClientID 
   */
  selectedClient = Client.getClientById(selectedClientID);
} 
else
{
  /* Call to jsp came from search page, traverse clients for 
   * client matching selectedClientID 
   */

  /* Pull the correct client from the list passed in*/
  for (int i = 0; i < clientResults.length; i ++)
  {
    if (clientResults[i].getClientId() == selectedClientID) 
	{
	   selectedClient = clientResults[i];
	}
  }
}  
  /* Make sure that the client was found in the passed list */
  if (selectedClient.getClientId() == -1) 
  {
    errorCode = "1";
    errorMessage = "Error occurred in ViewClient.jsp. Selected client was not in list of clients passed to this page." +
                   "Probably a problem with the session. Please retry the search.";
    redirectURL = "ElderhealthError.jsp?errorCode=" + errorCode + "&errorMessage=" + errorMessage;
    response.sendRedirect(redirectURL);
  }
  else
  {
    /* Client was found, display details */
%>
    Client ID: <%=selectedClient.getClientId()%><br>
	First Name: <%=selectedClient.getFirstName()%><br>
    Last Name: <%=selectedClient.getLastName()%><br>
	<form name="clientSearch" action="GetClientSchedule.jsp?clientID=selectedClient.getClientId()" method="get">
	  <input type="hidden" name="clientID" value=<%=selectedClientID%>>
	  <input type="button" value="View Schedule" onClick="submit()">
	  <input type="button" value="Edit Client" onclick="top.location='ClientManagement.jsp'">
	  <input type="button" value="Go Back" onClick="history.back();"><br><br>
	</form>
<%
  }
 //}
} 
catch (Exception e) 
{
    errorCode = "1";
    errorMessage = "Error occurred in ViewClient.jsp. Most likely error is that no client id was submitted to this page. Error is:" +
                   e.toString();
    redirectURL = "ElderhealthError.jsp?errorCode=" + errorCode + "&errorMessage=" + errorMessage;
    response.sendRedirect(redirectURL);
}
%>
	<br>
    </td></tr>
    <tr><td class="layoutLine" valign="middle" align="left" width="302" height="1" colSpan="4"></td></tr>
</table>

</body>
</html>